/* 选项卡插件
* @author Lunali
* @email lunali_blog@163.com
* @blog www.lunali.net
* @version 1.0  
*/
;(function($){
	
	var Tab = function(obj,options) {
		
		this.settings = $.extend({}, Tab.Defaults, options);
		
		this.xTabsNav = obj.find(this.settings.xTabsNav),  //指定对象下的导航
		this.xTabsBox = obj.find(this.settings.xTabsBox),	//指定对象下的内容块
		this.NavCurrent = this.settings.NavCurrent,
		this.width = this.xTabsBox.width(), //宽
		this.height = this.xTabsBox.height(), //高
		this.length = this.xTabsNav.children().length;  //控制按钮的长度
		this.iNow = 0;   //当前索引
		this.index = 0,  //索引
		this.delayRun = null,
		this.autoRun = null;
		this.isAgree = false;  //判断上一次是否执行完成，完成后再执行后一个效果
        
        this.init();
        
    }
	
	Tab.prototype = {
		init : function(){    /*初始化*/
		   (this.xTabsNav).children().removeClass((this.NavCurrent).substring(1)).eq(0).addClass((this.NavCurrent).substring(1));
		   
		   if(this.settings.animated == 'none' || this.settings.animated == 'fade'){
			  this.xTabsBox.children().hide().eq(0).show(); 
		   }else if(this.settings.animated == 'left'){
			  this.xTabsBox.css({'position':'relative'});
			  this.xTabsBox.children().css({'position':'absolute','left':0,'top':0});
			  for(var i=1; i<this.xTabsBox.children().length; i++){
				  this.xTabsBox.children().eq(i).css({'left':this.width});
			  }
		   }else if(this.settings.animated == 'top'){
			  this.xTabsBox.css({'position':'relative'});
			  this.xTabsBox.children().css({'position':'absolute','left':0,'top':0});
			  for(var i=1; i<this.xTabsBox.children().length; i++){
				  this.xTabsBox.children().eq(i).css({'top':this.height});
			  }
		   }
		   this._event();
		   
		   if(this.settings.autoPlay){
			  this._auto();
		   }
				 
		},
		_type : function(){     /*切换类型*/

			var _this = this;
		    this.isAgree = true; 
			
			(this.xTabsNav).children().removeClass((this.NavCurrent).substring(1)).eq(this.iNow).addClass((this.NavCurrent).substring(1));
			
			switch (this.settings.animated){
				 case 'none' : 	this.xTabsBox.children().eq(this.index).hide();  //无效果
								this.xTabsBox.children().eq(this.iNow).show(0,function(){
								   _this.isAgree = false;		
								});
								break;
				 case 'fade' : 	this.xTabsBox.children().eq(this.index).fadeOut(this.settings.fadeTime);   //淡入淡出
								this.xTabsBox.children().eq(this.iNow).fadeIn(this.settings.fadeTime,function(){
								  _this.isAgree = false;		
								});
								break;
				 case 'left':   if(this.iNow > this.index){    //往左切换
								  this.xTabsBox.children().eq(this.iNow).css({'left':this.width});
								  this.xTabsBox.children().eq(this.index).animate({left:-(this.width)},this.settings.animateTime);
								}else if(this.iNow < this.index){
								  this.xTabsBox.children().eq(this.iNow).css({'left':-(this.width)});
								  this.xTabsBox.children().eq(this.index).animate({left:this.width},this.settings.animateTime);
								}
								this.xTabsBox.children().eq(this.iNow).animate({left:0},this.settings.animateTime,function(){
								   _this.isAgree = false;	
								});
								break;
				 case 'top':    if(this.iNow > this.index){    //往上切换
								  this.xTabsBox.children().eq(this.iNow).css({'top':this.height});
								  this.xTabsBox.children().eq(this.index).animate({top:-(this.height)},this.settings.animateTime);
								}else if(this.iNow < this.index){
								  this.xTabsBox.children().eq(this.iNow).css({'top':-(this.height)});
								  this.xTabsBox.children().eq(this.index).animate({top:this.height},this.settings.animateTime);
								}
								this.xTabsBox.children().eq(this.iNow).animate({top:0},this.settings.animateTime,function(){
								  _this.isAgree = false;		
								});
								break;
			};
			this.index = this.iNow;
		},
		_event : function(){   /*绑定事件*/
			var _this = this;
			if(this.settings.overEvent){
				
				this.xTabsNav.children().hover(function(){
					
					if(_this.isAgree) return;
					
					_this.iNow = $(this).index();
					//delayRun = setTimeout(_this._type,_this.settings.delayTime);
					_this.delayRun = setTimeout(function(){
						_this._type();
					},_this.settings.delayTime);
				},function(){
					clearTimeout(_this.delayRun);
				})
			}else{
				this.xTabsNav.children().click(function(){
					if(_this.isAgree) return;
					
					_this.iNow = $(this).index();
					_this._type();
				})
			}
		},
		_next : function(){
			(this.iNow)++;
			if(this.iNow == this.length){
				this.iNow = 0;
			}
			this._type();
		},
		_auto : function(){
			var _this = this;
			
			this.autoRun = setInterval(function(){
			    _this._next();	
			},_this.settings.autoTime);
			
			this.xTabsNav.parent().on('mouseover',function(){
				clearInterval(_this.autoRun);
			}).on('mouseout',function(){
				_this.autoRun = setInterval(function(){
				   _this._next();	
				},_this.settings.autoTime);
			})
		}
	};
	
	/* 默认设置 */
	Tab.Defaults = {  
        xTabsNav : '.xTabs-Nav',   //导航
		xTabsBox : '.xTabs-Box',   //内容块父级
		NavCurrent : '.current',   //当前样式
		overEvent : true,   //默认事件为hover，若改为false，则为click事件
		animated : 'none',   //切换动画  （none/fade/left/top）
		fadeTime : 1000, //淡入淡出时间
		animateTime : 1000, //动画时间
		delayTime : 200,  //延迟时间  (建议200最佳)
		autoPlay : false,  //是否开启自动运行
		autoTime : 3000  //自动运行时间
	}
	
	$.fn.xTabs = function(options) {
		new Tab(this,options);
	};
		 
})(jQuery);


var BSTYUN = BSTYUN || {};
BSTYUN.voucher = {
		init: function(){
			var that = this;
			/*汇款凭证*/
			that.operateVoucher();
			/*上传汇款凭证照片*/
			that.uploadImg();
			/*修改凭证*/
			that.modifyVoucher();
			/*提交修改或者上传*/
			that.submitPayment();
			/* 删除图片 */
			that.deleteImgs();
			/* 隐藏百度统计小图标IE8下有问题 */
			that.hideBaiduIcon();
			/* 清楚汇款凭证的内容 */
			that.clearVoucher();
		},
		clearVoucher: function(){
			var $mytab = $("#myTab"),
				$inputs = $(".remittance_form").find("input[type!=hidden]");
			$mytab.on("click","li",function(){
				var $this = $(this),
					txt = $.trim($this.find("a").text());
				if(txt == "汇款凭证"){
					$inputs.attr("disabled",true).not("#updatePayment").val('');
				}else{
					$inputs.removeAttr("disabled");
				}
				
			});
		},
		hideBaiduIcon: function(){
			var $baidu = $(".baidutongji");
			$baidu.hide();
		},
		operateVoucher: function(){
			var $remittance = $("#remittance_infor"),
				$navs = $(".xTabs-Nav").find("li"),
				$form = $(".remittance_form").find("input"),
				$orderid = $form.filter("input[name='orderid']"),
				$remittance_time = $form.filter("input[name='remittance_time']"),
				$bankname = $form.filter("input[name='bankname']"),
				$remittance_infor = $(".remittance_infor"),
				$remit_box = $(".remit_box"),
				$return = $("#return_remit"),
				$remit_a = $(".xTabs-Nav li");
			var orderid = '',
				time = '',
				bankname = '';
			/*查看详情页面*/
			$remittance.on("click","a.view_detail",function(){
				var $this = $(this),
					paymentId = $.trim($this.attr("data-detailid"));
				$.ajax({
					type : "POST",
					url : "/finance/paymentDetail/"+paymentId,
					success : function(data) {
						var paymentVo = data.paymentVo;
						$('#DpaymentNum').text(paymentVo.paymentNum == null ? '' : paymentVo.paymentNum);
						$('#DorderNum').text(isOrderNull(paymentVo.orderNum));
						$('#DdealType').text(checkdealType(paymentVo.dealType));
						$('#DremitMoney').text(paymentVo.remitMoney);
						$('#DremitCompany').text(isNull(paymentVo.remitCompany));
						$('#DremitBank').text(isNull(paymentVo.remitBank));
						$('#DlinkMan').text(paymentVo.linkMan);
						$('#DlinkTell').text(paymentVo.linkTell);
						$('#DisCancel').text(paymentVo.isCancel);
					},
					error: function(){
						BSTYUN.dialog.warn("网络异常！请稍后再试")
					}
				});
				$remittance_infor.hide();
				$remit_box.show();
				$return.show();
			});
			$return.on("click",function(){
				var $this = $(this);
				$remittance_infor.show();
				$remit_box.hide();
				$this.hide();
			});
			$remit_a.on("click",function(){
				$return.trigger("click");
			});
		},
		uploadImg: function(){
			var $upload = $("#advImage"),
				$box = $("#fileuploader");
			var count = 0;
			$box.on("change",".advImage",function(){
				// 检查图片格式
				var f=document.getElementById("advImage").value;
				if(!/.(gif|jpg|jpeg|png|JPG|PNG)$/.test(f)) {
				    BSTYUN.dialog.warn("图片类型必须是.jpeg,jpg,png中的一种");
				    return false;
				}
				// 利用ajaxFileUpload.js 插件上传图片
				var $imgs = $("#showImg .small_img");
				
				if($imgs.length > 1){
					BSTYUN.dialog.warn("最多只能上传两张图片");
					return false;
				}
				$.ajaxFileUpload({
					url:"/uploadFile/image?attachType=1&objectName=payment",
					secureuri:false,
					fileElementId:"advImage",
					dataType:"json",
					success:function(data) {
						if(data.message == "上传文件成功") {
							BSTYUN.dialog.ok("恭喜您，上传凭证成功！");
							var imghtml = $("#showImg").html();
							var $newimg = $("<img />").attr({"src":data.path,"data-imgid":data.attachId}).addClass("small_img");
							var $newa = $("<a></a>").attr({"href":data.path,"target":"_blank"});
							$("#showImg").html(imghtml).append($newa.append($newimg));
							
							//$("#showImg").empty().append($("<img />").attr("src",data.path).addClass("small_img"));
							var preval = $('#attachId').val() == '' ? '': $('#attachId').val() + '|';
							$('#attachId').val( preval + data.attachId);
							
							/*绑定小图变大图*/
							baguetteBox.run('#showImg');
						}else{
							BSTYUN.dialog.warn(data.message);						
						}
					},
					error:function (data, status, e) {
						BSTYUN.dialog.warn("图片上传失败,请重新选择图片！");
					}
				});
				$('#advImage').replaceWith('<input type="file" name="advImage" id="advImage" class="advImage" title = "'+(count++)+'"/>');
				return false;
			});
		},
		deleteImgs: function(){
			var $imgbox = $("#showImg");
			$imgbox.on("mouseenter","a",function(){
				var $this = $(this);
				
				var paymentState = $('#paymentState').val();
				if(paymentState == 2) return;
				
				var $x = $('<span href="javascript:void;" class="img_close"></span>');
				$this.append($x);
				var imgid = $this.find(".small_img").attr("data-imgid");
				var paymentId = $('#paymentId').val();
				$x.on('click',function(event){
					var _this = $(this);
					BSTYUN.dialog.confirm("确定要删除这张照片吗？",function(){
						$.ajax({
							url: '/finance/deleteAttachById',
							type: 'post',
							dataType: 'json',
							data: {
								imgid: imgid,
								paymentId : paymentId 
							},
							success: function(data){
								if(data.isSuccess && data.isSuccess == 1){
									var $attachid = $("#attachId");
									var $imgs = $this.siblings('a').find('.small_img'),
										value = '';
									$imgs.each(function(){
										var $this = $(this),
											temple = $.trim($this.attr("data-imgid"));
										value == '' ? (value = temple):(value += '|'+temple);
									});
									$("#attachId").val(value);
									$this.remove();
								}else{
									BSTYUN.dialog.warn("图片删除失败，请稍后尝试！");
								}
							},
							error: function(){
								BSTYUN.dialog.warn("网络异常，请稍后尝试！");
							}
						});
					});
					return false;
				});
			}).on("mouseleave","a",function(){
				var $this = $(this);
				var $imgclose = $this.find('span'),
					size = $imgclose.length;
				if(!!size){
					$imgclose.remove();
				}
			});
		},
		modifyVoucher: function(){
			var $table = $("#remittance_infor"),
				$remit_a = $(".xTabs-Nav li");
			$table.on("click","a.operate_voucher",function(){
				var $this = $(this),
					paymentId = $this.attr("data-pmid"),
					state = $this.attr("data-pmst"),
					$show = $("#showImg");
				/*给提交按钮绑定数据，方便成功后修改数据*/
				$("#updatePayment").data("paymentid",paymentId);
				$.ajax({
					type : "POST",
					url : "/finance/paymentDetail/"+paymentId,
					success : function(data) { 
						var paymentVo = data.paymentVo,
							aList = data.aList;
						//放数据
						$('#paymentId').val(paymentVo.paymentId);
						$('#paymentNum').val(paymentVo.paymentNum);
						$('#orderNumber').val(isOrderNull(paymentVo.orderNum));
						//时间格式处理
						var remitTime = new Date();
						if(paymentVo.remitTime)
							remitTime = new Date(paymentVo.remitTime);
						remitTime = toDate(remitTime, "yyyy-MM-dd hh:mm:ss");
						$('#remitTime').val(remitTime);
						$('#remitBank').val(paymentVo.remitBank);
						
						var flg = false;
						if(state == 2) flg = true;
						
						$('#remitTime').prop("disabled",flg);
						$('#remitBank').prop("disabled",flg);
						$('#attachPath').prop("disabled",flg);
						$('#uploadFile').prop("disabled",flg);
						
						$("#advImage").prop("disabled",flg);
						$("#updatePayment").prop("disabled",flg);
						$('#attachId').val(paymentVo.attachIds);
						
						$('#paymentState').val(paymentVo.paymentState);
						
						if(aList == null){
							$show.empty();
						}else{
							$("#showImg").empty();
							$.each(aList,function(index,val){
								var $newimg = $("<img />").attr({"src":val.attachPath,"data-imgid":val.attachId}).addClass("small_img");
								var $newa = $("<a></a>").attr({"href":val.attachPath,"target":"_blank"});
								$("#showImg").append($newa.append($newimg));
								//$show.empty().append($("<img />").attr("src",val.attachPath).addClass("small_img"));
							});
							/*绑定小图变大图*/
							baguetteBox.run('#showImg');
						}
						//$(".small_img").showDetailImg();
					},
					error: function(){
						BSTYUN.dialog.warn("网络异常！请稍后再试")
					}
				});
				$remit_a.eq(1).trigger("click");
			});
		},
		submitPayment: function(){
			var paymentId = null,attachId = null,remitBank = null,remitTime = null,previd = null;
			var $submit = $("#updatePayment"),
				$time = $('#remitTime'),
				$bank = $('#remitBank');
			$submit.on("click",function(){
				paymentId = $('#paymentId').val();
				attachId = $('#attachId').val();
				remitBank = $bank.val();
				remitTime = $time.val();
				
				if(remitBank == "" || remitTime == "" || attachId.length <1){
					return BSTYUN.dialog.warn("请将该页面的数据进行完整填写！");
				}
				previd = $("#updatePayment").data("paymentid");
				var $td = $("#remittance_infor").find("td");
				$.ajax({
					type : "POST",
					url : "/finance/updatePayment/"+paymentId+"/"+attachId+"/"+remitBank+"/"+remitTime,
					success : function(data) { 
						if(data.isOk){
							BSTYUN.dialog.warn("已提交成功，请等待审核！",function(){
								$(".xTabs-Nav li").eq(0).trigger("click");
							});
							$.each($td,function(){
								var $this = $(this),
									id = $.trim($this.find("a:first").attr("data-pmid"));
								if(id == previd){
									$this.prevAll(".bankname").text(remitBank);
									$this.prevAll(".time").text(remitTime);
									$this.prevAll(".status").text("已上传");
									$this.find('a:first').text("修改凭证");
								}
							});
							//$(".small_img").showDetailImg();
							
						}else{
							BSTYUN.dialog.warn("未提交成功，请重新点击提交按钮！");
						}
					},
					error: function(){
						BSTYUN.dialog.warn("网络异常！请稍后再试");
					}
				});
			});
			
		}
}
var index = 0;
$(function(){
	$('.xTabsMain').xTabs({
		   animated : 'none',  //动画
		   overEvent : false,   //事件
		   NavCurrent : '.active',  //当前样式类名
		   autoPlay : false  //自动播放
		});
	BSTYUN.voucher.init();
});


//交易状态显示
function checkdealType(dealType) {
	if (dealType == "1") {
		return "充值";
	} else if (dealType == "2") {
		return "消费";
	} else if (dealType == "3") {
		return "提现";
	} else if (dealType == "4") {
		return "退款";
	} else if (dealType == "5") {
		return "其他";
	}
}

//是否为空判断
function isNull(data) {
	if (data == null) {
		return "";
	} else {
		return data;
	}
}

function isOrderNull(data){
	if (data == null) {
		return "充值";
	} else {
		return data;
	}
}
